<template>
    <div>
        <el-dialog v-model="props.dialogPrice" :title="newTitle" @close="handlePriceClose" width="500">
            <el-form ref="ProductPriceRef" :model="PriceForm" :rules="publicRules" label-width="auto"
                label-position="left" v-if="props.choseType === 1">
                <el-form-item required prop="resource">
                    <el-radio-group v-model="PriceForm.resource">
                        <el-radio :value="1">划线价</el-radio>
                        <el-radio :value="2">销售价</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item required prop="resourcePrice">
                    <el-radio-group v-model="PriceForm.resourcePrice">
                        <el-radio :value="1">改价</el-radio>
                        <el-radio :value="2">加价</el-radio>
                        <el-radio :value="0">
                            <el-tooltip class="box-item" effect="dark" content="被减值后为负数自动设置为0" placement="top">
                                <el-button link plain >减价 <el-icon color="#E6A23C"><Warning /></el-icon></el-button>
                            </el-tooltip>
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item :label="labelName(PriceForm.resourcePrice, 1)" required prop="several">
                    <el-input-number v-model="PriceForm.several" :min="0" :precision="2" style="width: 100%;" />
                </el-form-item>
            </el-form>
            <el-form ref="ProductWeightRef" :model="WeightForm" :rules="publicRules" label-width="auto"
                label-position="left" v-else-if="props.choseType === 2">
                <el-form-item required prop="resourcePrice">
                    <el-radio-group v-model="WeightForm.resourcePrice">
                        <el-radio :value="1">改重量</el-radio>
                        <el-radio :value="2">加重量</el-radio>
                        <el-radio :value="0">
                            <el-tooltip class="box-item" effect="dark" content="被减值后为负数自动设置为0" placement="top">
                                <el-button link plain >减重量 <el-icon color="#E6A23C"><Warning /></el-icon></el-button>
                            </el-tooltip>

                            </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item :label="labelName(WeightForm.resourcePrice, 2)" required prop="several">
                    <el-input-number v-model="WeightForm.several" :min="0" :precision="2" style="width: 100%;" />
                </el-form-item>
                <el-form-item required prop="unit" :label="WeightForm.resourcePrice == null ? '' : '重量单位'">
                    <el-select v-model="WeightForm.unit" placeholder="请选择" style="width: 100%;">
                        <el-option v-for="item in sys_weight_unit" :key="item.value" :label="item.label"
                            :value="item.value" />
                    </el-select>
                </el-form-item>
            </el-form>
            <el-form ref="ProductDimensionRef" :model="DimensionForm" :rules="publicRules" label-width="auto"
                label-position="left" v-else>
                <el-form-item label="长" required prop="length">
                    <el-input-number v-model="DimensionForm.length" :min="0" :precision="2" style="width: 100%;" />
                </el-form-item>

                <el-form-item label="宽" required prop="width">
                    <el-input-number v-model="DimensionForm.width" :min="0" :precision="2" style="width: 100%;" />
                </el-form-item>
                <el-form-item label="高" required prop="height">
                    <el-input-number v-model="DimensionForm.height" :min="0" :precision="2" style="width: 100%;" />
                </el-form-item>
            </el-form>
            <template #footer>
                <el-button type="primary" @click="handlePriceSure">确定</el-button>
                <el-button type="danger" @click="handlePriceClose">取消</el-button>
            </template>
        </el-dialog>
    </div>
</template>
<script setup>
import { ref, defineProps, defineEmits, watch, reactive } from "vue";
import { Warning } from "@element-plus/icons-vue";
const { proxy } = getCurrentInstance();
const emits = defineEmits(["handlePriceClose", "handlePriceSure"]);
const { sys_weight_unit } = proxy.useDict("sys_weight_unit");
const props = defineProps({
    //   默认隐藏
    dialogPrice: {
        type: Boolean,
        default: false,
    },
    choseType: {
        type: Number,
        default: 0,//0是批量修改包裹尺寸，1是批量修改价格，2是批量修改包裹重量
    },
});
const newTitle = ref('')

const PriceForm = ref({//价格
    resource: null,
    resourcePrice: null,
    several: 0
})
const WeightForm = ref({//重量
    resourcePrice: null,
    several: 0,
    unit: 'G'
})
const DimensionForm = ref({//尺寸单位
    length: null,//长
    width: null,//宽
    height: null//高
})

watch(
    () => props.dialogPrice,
    () => {
        if (!props.dialogPrice) return;
        if (props.choseType === 1) {
            newTitle.value = '批量修改价格'
        } else if (props.choseType === 2) {
            newTitle.value = '批量修改重量'
        } else {
            newTitle.value = '批量修改尺寸（cm）'
        }
    }
);
const handlePriceSure = () => {
    if (props.choseType === 1) {//批量修改价格
        proxy.$refs["ProductPriceRef"].validate((valid) => {
            if (!valid) return
            emits("handlePriceSure", PriceForm.value);
        })
    } else if (props.choseType === 2) {//批量修改重量
        proxy.$refs["ProductWeightRef"].validate((valid) => {
            if (!valid) return
            emits("handlePriceSure", WeightForm.value);
        })
    } else {//批量修改尺寸（cm）
        proxy.$refs["ProductDimensionRef"].validate((valid) => {
            if (!valid) return
            emits("handlePriceSure", DimensionForm.value);
        })
    }
}
const handlePriceClose = () => {
    emits("handlePriceClose");
    proxy.resetForm("ProductPriceRef");
    proxy.resetForm("ProductWeightRef");
    proxy.resetForm("ProductDimensionRef");
}
const publicRules = reactive({
    resource: [{ required: true, trigger: "blur", message: "请选择" }],
    resourcePrice: [{ required: true, trigger: "blur", message: "请选择" }],
    several: [{ required: true, trigger: "blur", message: "请输入" }],
    unit: [{ required: true, trigger: "blur", message: "请选择" }],
    length: [{ required: true, trigger: "blur", message: "请输入" }],
    width: [{ required: true, trigger: "blur", message: "请输入" }],
    height: [{ required: true, trigger: "blur", message: "请输入" }],
})
const labelName = (index, type) => {
    if (type === 1) {
        if (index === 1) {
            return '改价'
        } else if (index === 2) {
            return '加价'
        } else if (index === 0) {
            return '减价'
        }
    } else {
        if (index === 1) {
            return '改重量'
        } else if (index === 2) {
            return '加重量'
        } else if (index === 0) {
            return '减重量'
        }
    }
}
</script>
